package estore.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Product implements Comparable<Product>, Serializable { // 实现Comparable接口
    private static final long serialVersionUID = 1L;
    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存
    private Set<String> tags;

    public Product(String id, String name, double price, int stock,Set<String> tags) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.tags = tags == null ? new HashSet<>() : tags;
    }
    public Product(String id, String name, double price, int stock) {
        this(id, name, price, stock, new HashSet<>());
    }
    // Getters and Setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
    public int getStock() { return stock; }
    public void setStock(int stock) { this.stock = stock; }
    public Set<String> getTags() { return tags; }
    public void setTags(Set<String> tags) { this.tags = tags; }


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(String.format("ID: %s | 名称: %s | 价格: %.2f | 库存: %d", id, name, price, stock));
        if (!tags.isEmpty()) {
            sb.append(" | 标签: ").append(tags);}
        return sb.toString();
    }

    // 实现Comparable接口的compareTo方法，按商品ID字母顺序升序排列
    @Override
    public int compareTo(Product other) {
        return this.id.compareTo(other.id);
    }

    // 重写equals和hashCode，基于商品ID判断对象相等性
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}